package cloud.xiangyixiang.www.spring.boot.java.basics.service;

import java.util.Objects;
import java.util.function.Predicate;

/**
 * <p>
 * 断言性接口
 * </p>
 *
 * @author weixin
 * @date 2023/9/16 19:04
 */
@FunctionalInterface
public interface IPredicate<T> {

    boolean test(T t);

    default Predicate<T> and(Predicate<? super T> other) {
        Objects.requireNonNull(other);
        return (t) -> test(t) && other.test(t);
    }

    default Predicate<T> negate() {
        return (t) -> !test(t);
    }

    default Predicate<T> or(Predicate<? super T> other) {
        Objects.requireNonNull(other);
        return (t) -> test(t) || other.test(t);
    }

    static <T> Predicate<T> isEqual(Object targetRef) {
        return (null == targetRef)
                ? Objects::isNull
                : object -> targetRef.equals(object);
    }

}
